// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Eggman Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// Aliases
private alias 59 : TYPE_EGGMAN

// Function declarations
reserve function Eggman_Function135

// Static Values

// Tables
public table Eggman_table52
	-32, -40, -48, -64, -80, -96, -112, -120, -119, -120
end table


function Eggman_Function135
	object.ypos = object.value1
	Sin256(temp0, object.value2)
	temp0 <<= 9
	object.ypos += temp0
	object.value2 += 4
	object.value2 &= 255
end function


event ObjectMain
	switch object.state
	case 0
		temp0 = object.ixpos
		temp0 -= 352
		stage.newXBoundary1 = temp0
		temp0 = object.ixpos
		temp0 += 96
		stage.newXBoundary2 = temp0
		temp0 = object.xpos
		temp0 -= 0xC00000
		if object[0].xpos >= temp0
			object.animation = 1
			ResetObjectEntity(26, TypeName[Music Event], 0, 0, 0)
			object[26].priority = PRIORITY_ACTIVE
			object.value1 = object.ypos
			object.value12 = object.xpos
			object.value11 = object.value12
			object.value11 -= 0x1000000
			object.value10 = object.value11
			object.value10 += 0x800000
			object.value13 = object.ypos
			object.value13 += 0x260000
			object.value14 = object.ypos
			object.value14 += 0xF0000
			object.value4 = 144
			object.value2 = 128
			object.value15 = 7
			object.value7 = 8
			object.priority = PRIORITY_ACTIVE
			object.state++
		end if
		break
	case 1
		object.value1 -= 0xE000
		object.ypos -= 0xE000
		object.value4--
		if object.value4 == 0
			object.state = 2
		end if
		break
	case 2
		CallFunction(Eggman_Function135)
		if object.value2 == 64
			if object.value4 == 0
				object.value4++
			else
				object.state = 3
			end if
		end if
		if object.value4 == 1
			object.value6++
			switch object.value6
			case 1
				PlaySfx(SfxName[Fire Dash], 0)
				object.value5 = 9
				break
			case 3
				object.value5 = 10
				break
			case 6
				object.value5 = 11
				break
			case 9
				object.value5 = 12
				break
			case 13
				object.value5 = 13
				break
			case 17
				object.value5 = 14
				break
			case 22
				object.value5 = 15
				break
			case 25
				CreateTempObject(TypeName[Eggman Fireball1], 0, object.xpos, object.ypos)
				if object.direction == FLIP_NONE
					object[tempObjectPos].xpos -= 0x680000
					object[tempObjectPos].xvel = -0x40000
				else
					object[tempObjectPos].xpos += 0x680000
					object[tempObjectPos].xvel = 0x40000
				end if
				object[tempObjectPos].ypos -= 0x1D0000
				object[tempObjectPos].direction = object.direction
				break
			case 27
				object.value5 = 16
				break
			case 33
				object.value5 = 17
				break
			case 39
				object.value5 = 18
				break
			case 46
				object.value4 = 2
				object.value6 = 0
				break
			end switch
		end if
		break
	case 3
		object.value1 += 0xE000
		object.ypos += 0xE000
		object.value4++
		if object.xpos < object.value10
			temp0 = 36
			temp1 = -0x54000
		else
			temp0 = 84
			temp1 = -0x64000
		end if
		if object.value4 == temp0
			PlaySfx(SfxName[Small Fireball], 0)
			CreateTempObject(TypeName[Eggman Fireball2], 0, object.xpos, object.ypos)
			object[tempObjectPos].xvel = -0x1C000
			object[tempObjectPos].yvel = temp1
			CreateTempObject(TypeName[Eggman Fireball2], 0, object.xpos, object.ypos)
			object[tempObjectPos].xvel = 0x1C000
			object[tempObjectPos].yvel = temp1
		end if
		if object.value4 == 160
			if object[0].xpos < object.value10
				object.xpos = object.value11
				object.ypos = object.value13
				object.value1 = object.value13
			else
				object.xpos = object.value12
				object.ypos = object.value14
				object.value1 = object.value14
			end if
			if object[0].xpos < object.xpos
				object.direction = FLIP_NONE
			else
				object.direction = FLIP_X
			end if
			object.value2 = 128
			object.state = 1
		end if
		break
	case 4
		object.value4++
		if object.value4 == 120
			object.value15 = 8
		end if
		if object.value4 == 180
			object.value4 = 0
			object.animation = 5
			object.value9 = 0
			object.state++
			ResetObjectEntity(26, TypeName[Music Event], 1, 0, 0)
			object[26].priority = PRIORITY_ACTIVE
			temp0 = tileLayer[0].xsize
			temp0 <<= 7
			stage.newXBoundary2 = temp0
		end if
		break
	case 5
		object.value1 += 0x10000
		CallFunction(Eggman_Function135)
		if object.outOfBounds == 1
			object.type = TypeName[Blank Object]
			object.priority = PRIORITY_ACTIVE_BOUNDS
		end if
		break
	end switch
	if object[0].gravity == GRAVITY_GROUND
		object.value16 = 0
	end if
	if object.value7 != 0
		if object.value8 > 0
			object.value8--
			GetBit(temp0, object.value8, 0)
			if temp0 == 1
				SetPaletteEntry(0, 192, 0xE0E0E0)
			else
				SetPaletteEntry(0, 192, 0)
			end if
		end if
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			if object.value8 == 0
				CheckEqual(object[currentPlayer].animation, ANI_HURT)
				temp0 = checkResult
				CheckEqual(object[currentPlayer].animation, ANI_DYING)
				temp0 |= checkResult
				CheckEqual(object[currentPlayer].animation, ANI_DROWNING)
				temp0 |= checkResult
				if temp0 != 0
					if object.animation != 2
						object.animation = 2
						object.animationTimer = 0
					end if
				end if
				BoxCollisionTest(C_TOUCH, object.entityPos, -24, -24, 24, 22, currentPlayer, object[currentPlayer].value40, object[currentPlayer].value38, object[currentPlayer].value41, object[currentPlayer].value39)
				if checkResult == 1
					CallFunction(PlayerObject_CheckHit)
					if checkResult == 1
						object.value7--
						if object.value7 == 0
							player.score += 1000
							object.animation = 4
							object.animationTimer = 0
							object.value9 = 1
							object.value1 = object.ypos
							object.state = 4
							object.value6 = 0
							object.value5 = 0
							if object.value16 >= 7
								if stage.debugMode == 0
									CallNativeFunction2(SetAchievement, 5, 100)
								end if
							end if
						else
							object.animation = 3
							object.animationTimer = 0
							object.value8 = 32
							PlaySfx(SfxName[Boss Hit], 0)
							if currentPlayer == 0
								if object[0].gravity == GRAVITY_AIR
									object.value16++
								end if
							end if
						end if
					end if
				else
					if object.value6 != 0
						temp1 = object.value5
						temp1 -= 8
						if temp1 <= 7
							GetTableValue(temp0, temp1, Eggman_table52)
							if object.direction == FLIP_NONE
								BoxCollisionTest(C_TOUCH, object.entityPos, temp0, -32, -24, -29, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
							else
								FlipSign(temp0)
								BoxCollisionTest(C_TOUCH, object.entityPos, 24, -32, temp0, -29, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
							end if
							if checkResult == 1
								CallFunction(PlayerObject_FireHit)
							end if
						end if
					end if
				end if
			end if
		next
	end if
	if object.state != 5
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			temp0 = object[currentPlayer].collisionRight
			temp0 <<= 16
			temp0 += object[currentPlayer].xpos
			temp1 = stage.curXBoundary2
			temp1 <<= 16
			if temp0 > temp1
				object[currentPlayer].xvel = 0
				object[currentPlayer].speed = 0
				object[currentPlayer].xpos = temp1
				temp0 = object[currentPlayer].collisionRight
				temp0 <<= 16
				object[currentPlayer].xpos -= temp0
			end if
		next
	end if
	if object.value9 != 0
		temp0 = oscillation
		temp0 &= 7
		if temp0 == 0
			Rand(temp0, 48)
			temp0 -= 24
			temp0 <<= 16
			temp0 += object.xpos
			Rand(temp1, 48)
			temp1 -= 24
			temp1 <<= 16
			temp1 += object.ypos
			CreateTempObject(TypeName[Explosion], 0, temp0, temp1)
			object[tempObjectPos].drawOrder = 5
			PlaySfx(SfxName[Explosion], 0)
		end if
	end if
	if object.value15 == 8
		temp0 = oscillation
		temp0 &= 31
		if temp0 == 0
			CreateTempObject(TypeName[Eggman SmokePuff], 0, object.xpos, object.ypos)
			object[tempObjectPos].ypos -= 0x280000
			object[tempObjectPos].xvel -= 0x6000
			object[tempObjectPos].yvel -= 0xC000
		end if
	end if
end event


event ObjectDraw
	if object.value6 != 0
		DrawSpriteFX(object.value5, FX_FLIP, object.xpos, object.ypos)
	end if
	switch object.animation
	case 1
		temp0 = object.animationTimer
		temp0 >>= 3
		if saveRAM[495] == 0
			object.animationTimer++
			object.animationTimer %= 15
		endif
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	case 2
		temp0 = object.animationTimer
		temp0 &= 15
		temp0 >>= 3
		temp0 += 2
		object.animationTimer++
		if object.animationTimer == 50
			object.animationTimer = 0
			if object[0].animation != ANI_DYING
				object.animation = 1
			end if
		end if
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	case 3
		temp0 = object.animationTimer
		temp0 &= 1
		temp0 += 2
		object.animationTimer++
		if object.animationTimer == 50
			object.animationTimer = 0
			object.animation = 1
		end if
		DrawSpriteFX(4, FX_FLIP, object.xpos, object.ypos)
		break
	case 4
		temp0 = object.animationTimer
		temp0 >>= 5
		temp0 += 4
		temp0 %= 5
		object.animationTimer++
		object.animationTimer &= 63
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	case 5
		DrawSpriteFX(5, FX_FLIP, object.xpos, object.ypos)
		break
	end switch
	DrawSpriteFX(6, FX_FLIP, object.xpos, object.ypos)
	DrawSpriteFX(object.value15, FX_FLIP, object.xpos, object.ypos)
end event


event ObjectStartup
	CheckCurrentStageFolder("Zone05")
	if checkResult == 1
			if saveRAM[495] == 0
				LoadSpriteSheet("HTZ/Objects.gif")
			else
				LoadSpriteSheet("HTZ/ObjectsE.gif")
			endif
		SpriteFrame(-15, -10, 17, 7, 66, 108)
		SpriteFrame(-15, -10, 17, 7, 66, 116)
		SpriteFrame(-15, -10, 17, 7, 66, 124)
		SpriteFrame(-15, -10, 17, 7, 66, 132)
		SpriteFrame(-15, -10, 17, 7, 66, 140)
		SpriteFrame(-15, -10, 17, 7, 66, 148)
		SpriteFrame(-32, -12, 64, 45, 1, 210)
		SpriteFrame(-24, -36, 40, 24, 9, 186)
		SpriteFrame(-16, -20, 32, 8, 17, 132)
		SpriteFrame(-32, -32, 8, 6, 1, 124)
		SpriteFrame(-40, -33, 16, 7, 10, 124)
		SpriteFrame(-48, -33, 24, 8, 1, 54)
		SpriteFrame(-64, -33, 40, 8, 1, 63)
		SpriteFrame(-80, -33, 56, 8, 1, 72)
		SpriteFrame(-96, -33, 72, 8, 1, 81)
		SpriteFrame(-112, -33, 88, 8, 1, 90)
		SpriteFrame(-120, -33, 88, 8, 1, 99)
		SpriteFrame(-119, -33, 63, 8, 1, 108)
		SpriteFrame(-120, -32, 31, 6, 1, 117)
		if options.vsMode == 1
			foreach (TypeName[Eggman], arrayPos0, ALL_ENTITIES)
				object[arrayPos0].type = TypeName[Blank Object]
			next
		end if
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-15, -10, 17, 7, 1007, 34)
		SpriteFrame(-15, -10, 17, 7, 1007, 42)
		SpriteFrame(-15, -10, 17, 7, 1007, 50)
		SpriteFrame(-15, -10, 17, 7, 1007, 58)
		SpriteFrame(-15, -10, 17, 7, 1007, 66)
		SpriteFrame(-15, -10, 17, 7, 1007, 74)
		SpriteFrame(-32, -12, 64, 45, 415, 154)
		SpriteFrame(-24, -36, 40, 24, 423, 130)
		SpriteFrame(-16, -20, 32, 8, 480, 170)
		SpriteFrame(-32, -32, 8, 6, 877, 116)
		SpriteFrame(-40, -33, 16, 7, 886, 116)
		SpriteFrame(-48, -33, 24, 8, 982, 46)
		SpriteFrame(-64, -33, 40, 8, 966, 55)
		SpriteFrame(-80, -33, 56, 8, 950, 64)
		SpriteFrame(-96, -33, 72, 8, 934, 73)
		SpriteFrame(-112, -33, 88, 8, 919, 82)
		SpriteFrame(-120, -33, 88, 8, 919, 91)
		SpriteFrame(-119, -33, 63, 8, 877, 100)
		SpriteFrame(-120, -32, 31, 6, 877, 109)
	end if
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
